<template>
  <el-dialog
    :title="!dataForm.gsmLiveRecordId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品id" prop="psmId">
      <el-input v-model="dataForm.psmId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="psmName">
      <el-input v-model="dataForm.psmName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品展示" prop="psmImg">
      <el-input v-model="dataForm.psmImg" placeholder="商品展示"></el-input>
    </el-form-item>
    <el-form-item label="商品标题" prop="psmTitle">
      <el-input v-model="dataForm.psmTitle" placeholder="商品标题"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="psmPrice">
      <el-input v-model="dataForm.psmPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="直播活动id" prop="gsmLiveId">
      <el-input v-model="dataForm.gsmLiveId" placeholder="直播活动id"></el-input>
    </el-form-item>
    <el-form-item label="在线人数" prop="gsmLiveRecordViewsNumber">
      <el-input v-model="dataForm.gsmLiveRecordViewsNumber" placeholder="在线人数"></el-input>
    </el-form-item>
    <el-form-item label="关注人数" prop="gsmLiveRecordFollowersNumber">
      <el-input v-model="dataForm.gsmLiveRecordFollowersNumber" placeholder="关注人数"></el-input>
    </el-form-item>
    <el-form-item label="点赞人数" prop="gsmLiveRecordThumbNumber">
      <el-input v-model="dataForm.gsmLiveRecordThumbNumber" placeholder="点赞人数"></el-input>
    </el-form-item>
    <el-form-item label="下单人数" prop="gsmLiveRecordOrdersNumber">
      <el-input v-model="dataForm.gsmLiveRecordOrdersNumber" placeholder="下单人数"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="gsmLiveRecordStartTime">
      <el-input v-model="dataForm.gsmLiveRecordStartTime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="gsmLiveRecordEndTime">
      <el-input v-model="dataForm.gsmLiveRecordEndTime" placeholder="结束时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          gsmLiveRecordId: 0,
          psmId: '',
          psmName: '',
          psmImg: '',
          psmTitle: '',
          psmPrice: '',
          gsmLiveId: '',
          gsmLiveRecordViewsNumber: '',
          gsmLiveRecordFollowersNumber: '',
          gsmLiveRecordThumbNumber: '',
          gsmLiveRecordOrdersNumber: '',
          gsmLiveRecordStartTime: '',
          gsmLiveRecordEndTime: ''
        },
        dataRule: {
          psmId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          psmName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          psmImg: [
            { required: true, message: '商品展示不能为空', trigger: 'blur' }
          ],
          psmTitle: [
            { required: true, message: '商品标题不能为空', trigger: 'blur' }
          ],
          psmPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          gsmLiveId: [
            { required: true, message: '直播活动id不能为空', trigger: 'blur' }
          ],
          gsmLiveRecordViewsNumber: [
            { required: true, message: '在线人数不能为空', trigger: 'blur' }
          ],
          gsmLiveRecordFollowersNumber: [
            { required: true, message: '关注人数不能为空', trigger: 'blur' }
          ],
          gsmLiveRecordThumbNumber: [
            { required: true, message: '点赞人数不能为空', trigger: 'blur' }
          ],
          gsmLiveRecordOrdersNumber: [
            { required: true, message: '下单人数不能为空', trigger: 'blur' }
          ],
          gsmLiveRecordStartTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          gsmLiveRecordEndTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.gsmLiveRecordId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.gsmLiveRecordId) {
            this.$http({
              url: this.$http.adornUrl(`/live/liverecord/info/${this.dataForm.gsmLiveRecordId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.psmId = data.liveRecord.psmId
                this.dataForm.psmName = data.liveRecord.psmName
                this.dataForm.psmImg = data.liveRecord.psmImg
                this.dataForm.psmTitle = data.liveRecord.psmTitle
                this.dataForm.psmPrice = data.liveRecord.psmPrice
                this.dataForm.gsmLiveId = data.liveRecord.gsmLiveId
                this.dataForm.gsmLiveRecordViewsNumber = data.liveRecord.gsmLiveRecordViewsNumber
                this.dataForm.gsmLiveRecordFollowersNumber = data.liveRecord.gsmLiveRecordFollowersNumber
                this.dataForm.gsmLiveRecordThumbNumber = data.liveRecord.gsmLiveRecordThumbNumber
                this.dataForm.gsmLiveRecordOrdersNumber = data.liveRecord.gsmLiveRecordOrdersNumber
                this.dataForm.gsmLiveRecordStartTime = data.liveRecord.gsmLiveRecordStartTime
                this.dataForm.gsmLiveRecordEndTime = data.liveRecord.gsmLiveRecordEndTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/live/liverecord/${!this.dataForm.gsmLiveRecordId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'gsmLiveRecordId': this.dataForm.gsmLiveRecordId || undefined,
                'psmId': this.dataForm.psmId,
                'psmName': this.dataForm.psmName,
                'psmImg': this.dataForm.psmImg,
                'psmTitle': this.dataForm.psmTitle,
                'psmPrice': this.dataForm.psmPrice,
                'gsmLiveId': this.dataForm.gsmLiveId,
                'gsmLiveRecordViewsNumber': this.dataForm.gsmLiveRecordViewsNumber,
                'gsmLiveRecordFollowersNumber': this.dataForm.gsmLiveRecordFollowersNumber,
                'gsmLiveRecordThumbNumber': this.dataForm.gsmLiveRecordThumbNumber,
                'gsmLiveRecordOrdersNumber': this.dataForm.gsmLiveRecordOrdersNumber,
                'gsmLiveRecordStartTime': this.dataForm.gsmLiveRecordStartTime,
                'gsmLiveRecordEndTime': this.dataForm.gsmLiveRecordEndTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
